package com.ungao.framework.reflection;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 反射工具
 */
public class ReflectionUtils {

    /**
     * setter注入方法
     *
     * @param target
     * @param filedName
     * @param value
     * @param <T>
     * @throws NoSuchFieldException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static <T> void setter(T target, String filedName, Object value) throws NoSuchFieldException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Class<T> clazz = (Class<T>) target.getClass();
        Field field = clazz.getDeclaredField(filedName);
        String methodName = "set" + CaseConverter.toUpperCase(filedName);
        Method method = clazz.getDeclaredMethod(methodName, field.getType());
        method.setAccessible(true);
        method.invoke(target, value);
    }
}
